package cn.leetcode.easy;

import java.util.HashSet;
import java.util.Set;

/**
 * 给你一个由不同字符组成的字符串 allowed 和一个字符串数组 words 。如果一个字符串的每一个字符都在 allowed 中，
 * 就称这个字符串是 一致字符串 。
 * <p>
 * 请你返回 words 数组中 一致字符串 的数目。
 *
 * @author dunlingzi
 */
public class No1684 {
    
    public static void main(String[] args) {
        // allowed = "ab", words = ["ad","bd","aaab","baa","badab"]
        // allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]
        // allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]
        String allowed = "cad";
        String[] words = {"cc", "acd", "b", "ba", "bac", "bad", "ac", "d"};
        Solution1 solution = new Solution1();
        System.out.println(solution.countConsistentString(allowed, words));
    }
}


/**
 * 题解：将数组中的字符串元素去重再循环单个字符校验是否都被包含在目标字符串中，
 * 缺点：耗时间 耗内存  需要多次循环
 * 复杂度：
 */
class Solution {
    public int countConsistentString(String allowed, String[] words) {
        int num = 0;
        for (String word : words) {
            char[] cs = word.toCharArray();
            Set<String> s = new HashSet<>();
            for (char c : cs) {
                s.add(String.valueOf(c));
            }
            if (allowed.length() >= s.size()) {
                boolean result = true;
                for (String c : s) {
                    if (!allowed.contains(c)) {
                        result = false;
                        break;
                    }
                }
                if (result) {
                    num++;
                }
            }
        }
        return num;
    }
}

class Solution1 {
    public int countConsistentString(String allowed, String[] words) {
        int num = 0;
        for (String word : words) {
            boolean result = true;
            for (int i = 0; i < word.length(); i++) {
                String str = String.valueOf(word.charAt(i));
                if (!allowed.contains(str)) {
                    result = false;
                    break;
                }
            }
            if (result) {
                num++;
            }
        }
        return num;
    }
}